<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

<#if enableCache>
    <!-- 开启二级缓存 -->
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>

</#if>
<#if baseResultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseRM" type="${package.Entity}.${entity}">
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
<#list table.commonFields as field><#--生成公共字段 -->
    <result column="${field.name}" property="${field.propertyName}" />
</#list>
<#list table.fields as field>
<#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
    </resultMap>

</#if>
    <!-- 分页查询映射结果 -->
    <resultMap id="PageRM" type="${package.Entity}.${entity}PageDO">
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
<#list table.commonFields as field><#--生成公共字段 -->
<#if (logicDeleteFieldName!"") != field.name>
    <result column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
<#list table.fields as field>
<#if (logicDeleteFieldName!"") != field.name>
<#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
</#if>
</#if>
</#list>
    </resultMap>

    <!-- 分页查询映射结果 -->
    <resultMap id="ListRM" type="${package.Entity}.${entity}ListDO">
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
<#list table.commonFields as field><#--生成公共字段 -->
<#if (logicDeleteFieldName!"") != field.name>
    <result column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
<#list table.fields as field>
<#if (logicDeleteFieldName!"") != field.name>
<#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
</#if>
</#if>
</#list>
    </resultMap>
<#if baseColumnList>

    <sql id="Table_Name">
        ${table.name}
    </sql>

    <!-- 通用详情查询结果列 -->
    <sql id="Base_Detail">
        <#list table.fields as field>
            <#if (logicDeleteFieldName!"") != field.name>
        <#if field_has_next>
            t1.${field.name},
        <#else >
            t1.${field.name}
        </#if>
            </#if>
        </#list>
    </sql>

    <sql id="Base_Detail_Without_Alias">
        <#list table.fields as field>
            <#if (logicDeleteFieldName!"") != field.name>
        <#if field_has_next>
            ${field.name},
        <#else >
            ${field.name}
        </#if>
            </#if>
        </#list>
    </sql>

</#if>

    <sql id="queryCondition">
        <foreach collection="queryCondition" separator="and" open="and" item="item" index="index" close="">
            <if test="item.compare == 'in'">
                ${r' ${item.tableAlias}'}${r' ${item.fieldName}'}   in
                <foreach collection="item.fieldValue" separator="," open="(" item="valItem" index="valIndex" close=")">
                    ${r' #{valItem}'}
                </foreach>
            </if>
            <if test="item.compare != 'in'">
                ${r' ${item.tableAlias}'}${r' ${item.fieldName}'} ${r' ${item.compare}'} ${r' #{item.fieldValue}'}
            </if>
        </foreach>
    </sql>

    <select id="getPageList" resultMap="PageRM">
        select
        <include refid="Base_Detail_Without_Alias"/>
        from <include refid="Table_Name"/>
        <where>
            <include refid="queryCondition"/>
        </where>
    </select>

    <select id="getList" resultMap="ListRM">
        select
        <include refid="Base_Detail_Without_Alias"/>
        from <include refid="Table_Name"/>
        <where>
            <include refid="queryCondition"/>
        </where>
    </select>
</mapper>
